import {Cache} from '../../common/utils/Common';

export default {
    data: {
        // navbar 播放需要的参数
        currentPlay: {
            id: 0,
            title: '留言',
            cover: 'https://p1.music.126.net/8g2Pn9Xw1m7cFhxOlc3BMg==/109951167875709379.jpg?param=140y140',
            singer: '张学友',
            // 播放状态
            playing: false
        },
        // 头部推荐音乐
        headerMusics: [
            {
                img: 'https://p1.music.126.net/k4roR-yt7Ds2xhNmsyTzxA==/109951165125402135.jpg?param=130y130',
                desc: '二等天使'
            },
            {
                img: 'https://p1.music.126.net/fNe65vxh4Ta6gefSQc1xnw==/3254554425189372.jpg?param=130y130',
                desc: '爱的习惯'
            },
            {
                img: 'https://p1.music.126.net/Oix4KKx3qsuL8UGiLKcDiA==/43980465127856.jpg?param=130y130',
                desc: '好人'
            },
        ],
        // 喜爱音乐推荐
        loveMusics: [
            [
                {
                    id: 1,
                    cover: 'https://p1.music.126.net/__V3aKm7yV5HJCX_t64yxQ==/63771674423249.jpg?param=130y130',
                    title: '留言',
                    singer: '张学友',
                    desc: '释放自己',
                    playing: false
                },
                {
                    id: 2,
                    cover: 'https://p1.music.126.net/7SNr9GLslb3Y4ku-sVUn5A==/4418937232046263.jpg?param=130y130',
                    title: '负亲',
                    singer: '小肥',
                    desc: '',
                    playing: false
                },
                {
                    id: 3,
                    cover: 'https://p2.music.126.net/xczV2xdx3VRwSo9FWqq_3g==/109951163676387996.jpg?param=130y130',
                    title: '永远飞行模式',
                    singer: 'Dear Jane',
                    desc: '哪里只得我共你，不许你注定一人。',
                    playing: false
                }
            ],
            [
                {
                    id: 4,
                    cover: 'https://p2.music.126.net/3lrUXI96dmlEi2UgeIhOHg==/109951163076301483.jpg?param=130y130',
                    title: '他一定很爱你',
                    singer: '阿杜',
                    desc: '我不应该在车里，我应该在车底!~',
                    playing: false
                },
                {
                    id: 5,
                    cover: 'https://p1.music.126.net/jR_A_vL1ngAml2mb16L20w==/3320525116753321.jpg?param=130y130',
                    title: '罗生门',
                    singer: '麦浚龙 / 谢安琪',
                    desc: '忘了吧？未必吧',
                    playing: false
                },
                {
                    id: 6,
                    cover: 'http://p2.music.126.net/CUFDJr7kFgmjoI7YSiGm4A==/109951166863299666.jpg?param=130y130',
                    title: '我的回忆不是我的(粤)',
                    singer: '海明威',
                    desc: '',
                    playing: false
                }
            ]

        ],
        // carouse 轮播图
        carouse: [
            {
                img: 'https://p1.music.126.net/y8aR5rgWUaZdURpOuLOD0Q==/109951168010112359.jpg?imageView&quality=89'
            },
            {
                img: 'https://p1.music.126.net/ADuADqSGnw9BFYauFTbK0g==/109951168010106660.jpg?imageView&quality=89'
            },
            {
                img: 'https://p1.music.126.net/olbRkbn05pdzgYWtzmd39w==/109951168010072539.jpg?imageView&quality=89'
            },
            {
                img: 'https://p1.music.126.net/ISvpBdB1aceWcIcBDA4XYQ==/109951168010072479.jpg?imageView&quality=89'
            }
        ],
    },
    onInit() {
        let cache;
        if (!(cache = this.$app.$def.cache)) {
            this.$app.$def.cache = new Cache();
            cache = new Cache();
        }
        // 获取当前播放的歌曲，看看有没有与推荐列表重合
        let currentPlay = cache.get('currentPlay');
        if (currentPlay) {
            this.loveMusics.forEach(musics => {
                musics.forEach(music => {
                    if (music.id == currentPlay.id) {
                        music.playing = currentPlay.playing;
                    }
                })
            })
        }
    },
    onReady(){},
    /**
     * 当使用 nav 进行播放。
     * @param val 当前播放音乐的 id
     */
    navPlay(val) {
        this.currentPlay.id = val.detail;
        this.searchPlayFunc(true);
    },
    /**
     * 当使用 nav 停止播放。
     * @param val 当前播放音乐的 id
     */
    navStop(val) {
        this.currentPlay.id = val.detail;
        this.searchPlayFunc(false);
    },
    /**
     * 播放音乐 由 music_item 传入
     * @param val 音乐信息
     */
    playMusic(val) {
        // 2. 父组件接收了 music_item 传入的数据并将其列为正在播放的音乐，返回给 bottom_nav 进行同步
        let data = val.detail;
        Object.assign(this.currentPlay, data);
        // 在数组中开放 play
        this.searchPlayFunc(true);
    },
    /**
     * 停止音乐，由 music_item 传入
     * @param val 音乐信息
     */
    stopMusic(val) {
        let data = val.detail;
        Object.assign(this.currentPlay, data);
        this.searchPlayFunc(false);
    },
    /**
     * 查询播放的 music
     * <ul>
     *  <li> open 为 true，在 {#code loveMusics} 中找到同类 Music 并关闭其播放状态，而后打开当前播放音乐的状态。
     *  <li> open 为 false，关闭当前播放音乐
     *  <li> 该方法主要控制 music_item 的播放状态
     * @param open
     */
    searchPlayFunc(open) {
        let cache = this.$app.$def.cache;
        // 在数组中开放 play
        this.loveMusics.forEach(musics => {
            musics.forEach(music => {
                if (open) {
                    let id;
                    // 如果是开启播放状态，那么将同类的播放状态关闭
                    if ((id = music.id) != this.currentPlay.id) {
                        music.playing = false;
                        let data = cache.get(id);
                        if (data) {
                            data.playing = false;
                            cache.set(id, data);
                        }
                    }
                }
                // 由父类控制播放
                if (music.id == this.currentPlay.id) {
                    music.playing = open;
                    this.currentPlay.playing = open;
                    cache.set(this.currentPlay.id, this.currentPlay);
                }
            })
        });
    }
}



